package com.gltqe.mask;


import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;


import java.io.IOException;

/**
 * 自定义 Jackson序列化器
 *
 * @author gltqe
 * @date 2023/6/14 15:03
 **/
public class DataMaskSerializer extends JsonSerializer<Object> {

    private DataMask dataMask;

    private DataMaskHandle dataMaskHandle;

    public DataMaskSerializer(DataMaskHandle dataMaskHandle) {
        this.dataMaskHandle = dataMaskHandle;
    }

    /**
     * 序列化
     *
     * @author gltqe
     * @date 2023/6/15 16:20
     **/
    @Override
    public void serialize(Object o, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
        if (dataMask != null && o != null) {
            Object value = this.dataMaskHandle.dataMaskHandle(dataMask, String.valueOf(o));
            jsonGenerator.writeObject(value);
        }
    }

    /**
     * 设置数据脱敏注解
     *
     * @param dataMask
     * @author gltqe
     * @date 2023/6/15 15:58
     **/
    public void setDataMask(DataMask dataMask) {
        this.dataMask = dataMask;
    }


    /**
     * 获取数据脱敏处理器
     *
     * @return: org.gltqe.dataMask.DataMaskHandle
     * @author gltqe
     * @date 2023/6/16 9:49
     **/
    public DataMaskHandle getDataMaskHandle() {
        return dataMaskHandle;
    }

}
